local function getupvaluetable(func, inname)
    local i = 1
    while true do
        local name, value = debug.getupvalue(func, i)
        if name == nil then
            return
        end
        
        if name == inname then
            return i,value
        end
        
        i=i+1
    end
end

if not _P then
    print "inject error!"
    return
end

local lua = _P.lua
local _, add_agent_member = getupvaluetable(lua.handler.add_agent_member,'add_agent_member')
local _, get_account_first_created_club = getupvaluetable(lua.handler.add_agent_member,'get_account_first_created_club')
local _, error_code = getupvaluetable(lua.handler.add_agent_member,'error_code')
local _, club_storage = getupvaluetable(lua.handler.can_join_club_room,'club_storage')
local _, agent_coll = getupvaluetable(club_storage.mark_agent_member,'agent_coll')

club_storage.is_agent_member = function(uid)
	local ok, res = agent_coll:findOne({id=0, uid_list=uid})
    dbglog("is_agent_member:", ok, res)
    if ok and res then
        return true
    end
    return false
end

lua.handler.add_agent_member = function(uid, agent_uid)
	dbglog("add_agent_member:", uid, agent_uid)

	if club_storage.is_agent_member(uid) then
		return 0
	end

	local club = get_account_first_created_club(agent_uid)
    if not club then
        return error_code.CLUB_NOT_CREATED
    end

    return add_agent_member(club, uid)
end